<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改物料')"/>
    <style>
        [v-cloak] {
            display: none;
        }
    </style>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-itemInfo-edit" th:object="${itemInfo}">
        <input id="treeId" name="itemTypeId" type="hidden"/>
        <div class="form-group">
            <label class="col-sm-3 control-label ">物料分类：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input class="form-control" type="text" id="treeName" onclick="selectItemTypeTree()"
                           readonly="true">
                    <span class="input-group-addon" onclick="selectItemTypeTree()"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <input id="itemId" name="itemId" th:field="*{itemId}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">物料编码：</label>
            <div class="col-sm-8">
                <input id="itemCode" name="itemCode" th:field="*{itemCode}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">物料名称：</label>
            <div class="col-sm-8">
                <input id="itemName" name="itemName" th:field="*{itemName}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">型号：</label>
            <div class="col-sm-8">
                <input id="model" name="model" th:field="*{model}" class="form-control" type="text">
            </div>
        </div>
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label">规格（容量）：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <select id="itemSpec" name="itemSpec" class="form-control" @change="getQuantity()" required>-->
<!--                    <template v-for="(item, index) in specs" :key="index">-->
<!--                        <option v-if="item == specId" selected="selected" :value="item" v-cloak>{{item}}</option>-->
<!--                        <option v-else :value="item" v-cloak>{{item}}</option>-->
<!--                    </template>-->
<!--                </select>-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label">规格（数量）：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <select id="itemSpecificationId" name="itemSpecificationId" class="form-control">-->
<!--                    <template v-for="(item, index) in quantities" :key="index">-->
<!--                        <option v-if="item.itemQuantity == quantityId" selected="selected" :value="item.itemSpecificationId" v-cloak>{{item.itemQuantity}}</option>-->
<!--                        <option v-else :value="item.itemSpecificationId" v-cloak>{{item.itemQuantity}}</option>-->
<!--                    </template>-->
<!--                </select>-->
<!--            </div>-->
<!--        </div>-->
        <div class="form-group">
            <label class="col-sm-3 control-label">单位：</label>
            <div class="col-sm-8">
                <input id="unit" name="unit" th:field="*{unit}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">库存预警数：</label>
            <div class="col-sm-8">
                <input id="warn" name="warn" th:field="*{warn}" class="form-control" type="number" required>
            </div>
        </div>
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label">质量：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <select id="itemQuality" name="itemQuality" class="form-control">-->
<!--                    <template v-for="(item, index) in qualities" :key="index">-->
<!--                        <option v-if="item.id == qualityId" selected="selected" :value="item.id" v-cloak>{{item.text}}</option>-->
<!--                        <option v-else :value="item.id" v-cloak>{{item.text}}</option>-->
<!--                    </template>-->
<!--                </select>-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label">长 (mm)：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <input id="length" name="length" th:field="*{length}" class="form-control" type="number">-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label">宽 (mm)：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <input id="width" name="width" th:field="*{width}" class="form-control" type="number">-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label">高 (mm)：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <input id="hight" name="hight" th:field="*{hight}" class="form-control" type="number">-->
<!--            </div>-->
<!--        </div>-->
        <div class="form-group">
            <label class="col-sm-3 control-label">保质期 (天)：</label>
            <div class="col-sm-8">
                <input id="dateWarning" name="dateWarning" th:field="*{dateWarning}" class="form-control" type="number" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">最长存放期 (天)：</label>
            <div class="col-sm-8">
                <input id="periodTime" name="periodTime" th:field="*{periodTime}" class="form-control" type="number" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">修改人卡号一</label>
            <div class="col-sm-8">
                <input id="cardNoOne" name="cardNoOne" th:field="*{cardNoOne}" class="form-control" type="password" required>
            </div>
        </div>
    </form>
    <form class="form-horizontal m" th:object="${itemInfo}">
        <input th:field="*{itemQuality}" type="hidden" id="hiddenQuality">
        <input th:field="*{itemSpec}" type="hidden" id="hiddenSpec">
        <input th:field="*{itemQuantity}" type="hidden" id="hiddenQuantity">
    </form>
</div>
<div th:include="include::footer"></div>
<script type="text/javascript">
    var prefix = ctx + "system/itemInfo";
    $(function () {
        var itemTypeId = '[[${itemInfo.itemTypeId}]]';
        var itemTypeName = '[[${itemInfo.itemTypeName}]]';
        $("#treeId").val(itemTypeId);
        $("#treeName").val(itemTypeName);
    })

    $("#form-itemInfo-edit").validate({
        onkeyup: false,
        rules: {
            cardNoOne: {
                minlength: 2,
                maxlength: 20,
                remote: {
                    url: ctx + "system/operator/checkOperatorCard",
                    type: "post",
                    dataType: "json",
                    data: {
                        "operatorCard": function(){
                            return $.common.trim($("#cardNoOne").val());
                        }
                    },
                    dataFilter: function (data, type){
                        if(data === "1"){
                            return $.validate.unique("0");
                        }else{
                            return $.validate.unique("1");
                        }
                    }
                }
            }
        },
        messages: {
            "cardNoOne": {
                remote: "此卡号未录入，请联系管理员录入!"
            }
        },
        focusCleanup: true
    });

    function submitHandler(){
        if ($.validate.form()){
            $.operate.save(prefix + "/edit", $('#form-itemInfo-edit').serialize());
        }
    }

    /*部门管理-新增-选择父部门树*/
    function selectItemTypeTree(){
        var options = {
            title: '物料分类选择',
            width: "380",
            url: ctx + "system/itemType" + "/selectItemTypeTree/0",
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero){
        var body = layer.getChildFrame('body', index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        layer.close(index);
    }

    var app = new Vue({
        el: '.wrapper',
        data: {
            qualities: [
                {id: "0", text: "待检"},
                {id: "1", text: "合格"},
                {id: "2", text: "不合格"}
            ],
            qualityId: '',
            specId: '',
            quantityId: '',
            specs: [],
            quantities: []
        },
        methods: {
            getSpecs:function(){
                var that = this;
                $.ajax({
                    cache: true,
                    type: "POST",
                    url: "/ItemSpecification/specList",
                    data: {},
                    async: false,
                    error: function(request){
                        $.modal.alertError("系统错误");
                    },
                    success: function(data){
                        var arr = [];
                        for(var i = 0; i < data.length; i++){
                            if(arr.indexOf(data[i].itemSpec) == -1){
                                arr.push(data[i].itemSpec)
                            }
                        }
                        that.specs = Array.from(new Set(arr));
                    }
                });
            },
            // getQuantity(){
            //     var that = this;
            //     var currentSel = document.getElementById('itemSpec');
            //     var index = currentSel.selectedIndex;
            //     var value = currentSel.options[index].value;
            //     if(value != ''){
            //         that.show = true;
            //         $.ajax({
            //             cache: true,
            //             type: "POST",
            //             url: "/ItemSpecification/quantityList",
            //             data: {
            //                 itemSpec: value
            //             },
            //             async: false,
            //             error: function(request){
            //                 $.modal.alertError("系统错误");
            //             },
            //             success: function(data){
            //                 that.quantities = data;
            //             }
            //         });
            //     }
            // }
        },
        created(){
            this.qualityId = document.querySelector('#hiddenQuality').value;
            this.specId = document.querySelector('#hiddenSpec').value;
            this.quantityId = document.querySelector('#hiddenQuantity').value;
            this.getSpecs();
        },
        mounted(){
            // this.getQuantity();
        }
    })
</script>
</body>
</html>
